package org.deschutter.parsers;

import org.deschutter.URLReader;
import org.deschutter.constants.Config;
import org.deschutter.init.ScimitarBot;
import org.deschutter.scans.*;

import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;


/**
 * User: Berten De Schutter
 * Date: 30-Jun-2009
 * Time: 15:59:18
 */
public abstract class Parser implements Config {

    public static ArrayList<String> getScanUrls(String message) {
        Pattern compiledRegex;
        Matcher regexMatcher;
        ArrayList<String> lijst = new ArrayList<String>();


        try {
            compiledRegex = Pattern.compile("scan_id=[a-z0-9]+");
            regexMatcher = compiledRegex.matcher(message);

            while (regexMatcher.find()) {
                lijst.add(PLANETARION_URL + SCAN_URL + regexMatcher.group());
            }

            compiledRegex = Pattern.compile("scan_grp=[a-z0-9]+");
            regexMatcher = compiledRegex.matcher(message);

            while (regexMatcher.find()) {
                lijst.add(PLANETARION_URL + SCAN_URL + regexMatcher.group());
            }
        } catch (PatternSyntaxException ex) {
            System.out.println("You have an error in your regular expression:\n" +
                    ex.getDescription());
        }

        return lijst;
    }

    public static void parse(ArrayList<String> scanurls, BigDecimal userID, ScimitarBot bot) {
        for (String url : scanurls) {

            if (url.contains("scan_grp")) {
                Parser.retreiveScansFromGroup(url, userID, bot);
            } else {
                Parser.parseScan(url, userID, bot);
            }
        }
    }

    public static void retreiveScansFromGroup(String url, BigDecimal userID, ScimitarBot bot) {
        Pattern compiledRegex;
        Matcher regexMatcher;

        try {
            String page = URLReader.get(url);
            compiledRegex = Pattern.compile("scan_id=[a-z0-9]+");
            regexMatcher = compiledRegex.matcher(page);
            while (regexMatcher.find()) {
                Parser.parseScan(PLANETARION_URL + SCAN_URL + regexMatcher.group(), userID, bot);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (PatternSyntaxException ex) {
            System.out.println("You have an error in your regular expression:\n" +
                    ex.getDescription());
        }
    }

    public static Scan parseScan(String url, BigDecimal userID, ScimitarBot bot) {
        Pattern compiledRegex;
        Matcher regexMatcher;
        Scan scan = null;
        HashMap scanInfoMap;
        try {
            String scanPage = URLReader.get(url);
            // Get Scan ID
            String scanID = getID(url);

            compiledRegex = Pattern.compile("([^>]+) on (\\d+)\\:(\\d+)\\:(\\d+) in tick (\\d+)");
            regexMatcher = compiledRegex.matcher(scanPage);

       /*     while (regexMatcher.find()) {
                if (regexMatcher.group(1).equals("Planet Scan")) {
                    scan = new Planet(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                } else if (regexMatcher.group(1).equals("Development Scan")) {
                    scan = new Development(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                } else if (regexMatcher.group(1).equals("Unit Scan")) {
                    scan = new Unit(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                } else if (regexMatcher.group(1).equals("News Scan")) {
                    scan = new News(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                } else if (regexMatcher.group(1).equals("Incomming Scan")) {
                    scan = new IncommingScan(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                } else if (regexMatcher.group(1).equals("Jumpgate Probe")) {
                    scan = new Jumpgate(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                } else if (regexMatcher.group(1).equals("Advanced Unit Scan")) {
                    scan = new AdvancedUnit(scanID, new BigDecimal(regexMatcher.group(5)), regexMatcher.group(2), regexMatcher.group(3), regexMatcher.group(4), scanPage, userID, bot.getSession());
                }
            }*/

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return scan;
    }

    public static String getID(String url) {
        String[] a = url.split("=");
        return a[1];
    }


}
